package com.feihong.yw189.bluetooth.utils.djlm.DJLMReader3.reader;

import android.util.Log;

import com.feihong.yw189.bluetooth.utils.djlm.DJLMReader3.websocket.ThreadType;
import com.feihong.yw189.bluetooth.utils.djlm.DJLMReader3.websocket.WebSocket;
import com.feihong.yw189.bluetooth.utils.djlm.DJLMReader3.websocket.WebSocketException;
import com.feihong.yw189.bluetooth.utils.djlm.DJLMReader3.websocket.WebSocketFrame;
import com.feihong.yw189.bluetooth.utils.djlm.DJLMReader3.websocket.WebSocketListener;
import com.feihong.yw189.bluetooth.utils.djlm.DJLMReader3.websocket.WebSocketState;

import java.util.List;
import java.util.Map;

/**
 * Created by Sabirjan on 2017/10/27.
 * DJLMReader.
 * 飞宏电子
 */

public class DJLMWebSocketListener implements WebSocketListener {

    private static final String TAG = "蓝牙";
    NewDJLMReader2 djlmReader2;
    public DJLMWebSocketListener(NewDJLMReader2 djlmReader2)
    {
        this.djlmReader2 = djlmReader2;
    }
    @Override
    public void onStateChanged(WebSocket websocket, WebSocketState newState) throws Exception {
        Log.d(TAG, "onStateChanged() called with: websocket = [" + websocket + "], newState = [" + newState + "]");
    }

    @Override
    public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
        Log.d(TAG, "onConnected() called with: websocket = [" + websocket + "], headers = [" + headers + "]");
    }

    @Override
    public void onConnectError(WebSocket websocket, WebSocketException cause) throws Exception {
        Log.d(TAG, "onConnectError() called with: websocket = [" + websocket + "], cause = [" + cause + "]");
        djlmReader2.mHandler.sendEmptyMessage(ErrorCode.ERROR_CODE8);
    }

    @Override
    public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
        Log.d(TAG, "onDisconnected() called with: websocket = [" + websocket + "], serverCloseFrame = [" + serverCloseFrame + "], clientCloseFrame = [" + clientCloseFrame + "], closedByServer = [" + closedByServer + "]");
        if(!djlmReader2.isSuccess)
        djlmReader2.mHandler.sendEmptyMessage(ErrorCode.ERROR_CODE7);
    }

    @Override
    public void onFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
    //    Log.d(TAG, "onFrame() called with: websocket = [" + websocket + "], frame = [" + frame + "]");
    }

    @Override
    public void onContinuationFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
    //    Log.d(TAG, "onContinuationFrame() called with: websocket = [" + websocket + "], frame = [" + frame + "]");
    }

    @Override
    public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
     //   Log.d(TAG, "onTextFrame() called with: websocket = [" + websocket + "], frame = [" + frame + "]");
    }

    @Override
    public void onBinaryFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
    //    Log.d(TAG, "onBinaryFrame() called with: websocket = [" + websocket + "], frame = [" + frame + "]");
    }

    @Override
    public void onCloseFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
    //    Log.d(TAG, "onCloseFrame() called with: websocket = [" + websocket + "], frame = [" + frame + "]");
    }

    @Override
    public void onPingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
   //     Log.d(TAG, "onPingFrame() called with: websocket = [" + websocket + "], frame = [" + frame + "]");
    }

    @Override
    public void onPongFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
    //    Log.d(TAG, "onPongFrame() called with: websocket = [" + websocket + "], frame = [" + frame + "]");
    }

    @Override
    public void onTextMessage(WebSocket websocket, String text) throws Exception {

        djlmReader2.doWebSocket(text);
        websocket.flush();
    }

    @Override
    public void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception {
        Log.d(TAG, "onBinaryMessage() called with: websocket = [" + websocket + "], binary = [" + binary + "]");
        if(binary.length>1200)
            djlmReader2.success(binary);
        else
            djlmReader2.doWebSocket(new String(binary));
        websocket.flush();
    }

    @Override
    public void onSendingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
     //   Log.d(TAG, "onSendingFrame() called with: websocket = [" + websocket + "], frame = [" + frame + "]");
    }

    @Override
    public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {
     //   Log.d(TAG, "onFrameSent() called with: websocket = [" + websocket + "], frame = [" + frame + "]");
    }

    @Override
    public void onFrameUnsent(WebSocket websocket, WebSocketFrame frame) throws Exception {
    //    Log.d(TAG, "onFrameUnsent() called with: websocket = [" + websocket + "], frame = [" + frame + "]");
    }

    @Override
    public void onThreadCreated(WebSocket websocket, ThreadType threadType, Thread thread) throws Exception {
    //    Log.d(TAG, "onThreadCreated() called with: websocket = [" + websocket + "], threadType = [" + threadType + "], thread = [" + thread + "]");
    }

    @Override
    public void onThreadStarted(WebSocket websocket, ThreadType threadType, Thread thread) throws Exception {
     //   Log.d(TAG, "onThreadStarted() called with: websocket = [" + websocket + "], threadType = [" + threadType + "], thread = [" + thread + "]");
    }

    @Override
    public void onThreadStopping(WebSocket websocket, ThreadType threadType, Thread thread) throws Exception {
      //  Log.d(TAG, "onThreadStopping() called with: websocket = [" + websocket + "], threadType = [" + threadType + "], thread = [" + thread + "]");
    }

    @Override
    public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
        Log.d(TAG, "onError() called with: websocket = [" + websocket + "], cause = [" + cause + "]");
    }

    @Override
    public void onFrameError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) throws Exception {
     //   Log.d(TAG, "onFrameError() called with: websocket = [" + websocket + "], cause = [" + cause + "], frame = [" + frame + "]");
    }

    @Override
    public void onMessageError(WebSocket websocket, WebSocketException cause, List<WebSocketFrame> frames) throws Exception {
        Log.d(TAG, "onMessageError() called with: websocket = [" + websocket + "], cause = [" + cause + "], frames = [" + frames + "]");
    }

    @Override
    public void onMessageDecompressionError(WebSocket websocket, WebSocketException cause, byte[] compressed) throws Exception {
        Log.d(TAG, "onMessageDecompressionError() called with: websocket = [" + websocket + "], cause = [" + cause + "], compressed = [" + compressed + "]");
    }

    @Override
    public void onTextMessageError(WebSocket websocket, WebSocketException cause, byte[] data) throws Exception {
        Log.d(TAG, "onTextMessageError() called with: websocket = [" + websocket + "], cause = [" + cause + "], data = [" + data + "]");
    }

    @Override
    public void onSendError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) throws Exception {
        Log.d(TAG, "onSendError() called with: websocket = [" + websocket + "], cause = [" + cause + "], frame = [" + frame + "]");
    }

    @Override
    public void onUnexpectedError(WebSocket websocket, WebSocketException cause) throws Exception {
        Log.d(TAG, "onUnexpectedError() called with: websocket = [" + websocket + "], cause = [" + cause + "]");
    }

    @Override
    public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception {
        Log.d(TAG, "handleCallbackError() called with: websocket = [" + websocket + "], cause = [" + cause + "]");
    }

    @Override
    public void onSendingHandshake(WebSocket websocket, String requestLine, List<String[]> headers) throws Exception {
        Log.d(TAG, "onSendingHandshake() called with: websocket = [" + websocket + "], requestLine = [" + requestLine + "], headers = [" + headers + "]");

    }
}
